<script>

export default {
  name: 'UserinfoPassword',
  data () {
    return {
      show: false,
      formData: {
        oldPassword: '',
        password: '',
        confirmPassword: ''
      },
      rules: {
        oldPassword: [{ required: true, message: '请输入原密码' }],
        password: [{ required: true, message: '请输入新密码' }],
        confirmPassword: [
          { required: true, message: '请再次输入密码' },
          {
            validator: (rule, value, callback) => {
              if (value === this.formData.password) {
                callback()
              } else {
                callback(new Error('两次密码不一致'))
              }
            }
          }
        ]
      }
    }
  },
  methods: {
    init () {
      this.show = true
    },
    submit () {
      this.$refs.passwordForm.validate(valid => {
        if (valid) {
          this.$axios.post('userinfo/update/password', this.formData).then(() => {
            this.$alert('密码修改成功，请重新登录！', '提示', { type: 'success' }).then(() => {
              this.show = false
              this.$emit('close')
            })
          }).catch(e => e)
        }
      })
    }
  }
}
</script>

<template>
<el-dialog title="修改密码" :visible.sync="show">
  <el-form ref="passwordForm" :model="formData" :rules="rules" label-width="100px">
    <el-form-item label="原密码" prop="oldPassword">
      <el-input v-model="formData.oldPassword" placeholder="请输入原密码" show-password/>
    </el-form-item>
    <el-form-item label="新密码" prop="password">
      <el-input v-model="formData.password" placeholder="请输入新密码" show-password/>
    </el-form-item>
    <el-form-item label="确认密码" prop="confirmPassword">
      <el-input v-model="formData.confirmPassword" placeholder="请再次输入新密码" show-password/>
    </el-form-item>
    <el-form-item>
      <el-button @click="submit">提交</el-button>
    </el-form-item>
  </el-form>
</el-dialog>
</template>

<style scoped lang="scss">

</style>
